package com.oracle.lingshangejia.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseFragment;
import com.oracle.lingshangejia.bean.RollInBean;
import com.oracle.lingshangejia.bean.RollOutBean;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;

/**
 * 转诊记录S
 */
public class ReferralRecordsFragment extends BaseFragment implements View.OnClickListener {
    private int jmid;
    private int jzid;
    private TextView tv_referal_unit;//转入单位
    private TextView tv_refer_result;//检查结果
    private TextView tv_refer_fangan;//方案
    private TextView tv_refer_doctor;//转诊医生
    private TextView tv_refer_phone;//电话
    private TextView tv_refer_date1;//转入时间
    private TextView tv_out_unit;//转出单位
    private TextView tv_out_yinxiang;//初步印象
    private TextView tv_out_xianbs;//现病史
    private TextView tv_out_jiwangshi;//既往史
    private TextView tv_out_in_unit;//转入单位
    private TextView tv_out_jingguo;//治疗经过
    private TextView tv_out_doctor;//转诊医生
    private TextView tv_out_phone;//联系电话
    private TextView tv_out_date2;//时间
    private LinearLayout ll_roll_in;
    private LinearLayout ll_roll_out;
    private boolean haverollin;
    private boolean haverollout;
    private ScrollView scollview_sx;
    private ImageView iv_sx_img;
    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getNetWork();
        getNetWork1();
    }

    @Override
    protected void initView() {
        jmid=getArguments().getInt("jmid");
        jzid=getArguments().getInt("jzid");
    }

    @Override
    protected void onBindViewBefore(View view) {
        tv_referal_unit=view.findViewById(R.id.tv_referal_unit);
        tv_refer_result=view.findViewById(R.id.tv_refer_result);
        tv_refer_fangan=view.findViewById(R.id.tv_refer_fangan);
        tv_refer_doctor=view.findViewById(R.id.tv_refer_doctor);
        tv_refer_phone=view.findViewById(R.id.tv_refer_phone);
        tv_refer_date1=view.findViewById(R.id.tv_refer_date1);
        tv_out_unit=view.findViewById(R.id.tv_out_unit);
        tv_out_yinxiang=view.findViewById(R.id.tv_out_yinxiang);
        tv_out_xianbs=view.findViewById(R.id.tv_out_xianbs);
        tv_out_jiwangshi=view.findViewById(R.id.tv_out_jiwangshi);
        tv_out_in_unit=view.findViewById(R.id.tv_out_in_unit);
        tv_out_jingguo=view.findViewById(R.id.tv_out_jingguo);
        tv_out_doctor=view.findViewById(R.id.tv_out_doctor);
        tv_out_phone=view.findViewById(R.id.tv_out_phone);
        tv_out_date2=view.findViewById(R.id.tv_out_date2);
        ll_roll_in=view.findViewById(R.id.ll_roll_in);
        ll_roll_out=view.findViewById(R.id.ll_roll_out);
        scollview_sx=view.findViewById(R.id.scollview_sx);
        iv_sx_img=view.findViewById(R.id.iv_sx_img);
    }
    /**
     * 转出请求网络
     */
    private void getNetWork() {
        int ysid= (int) SpUtils.get(getActivity(),"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmId",jmid);
        map.put("jzid",jzid);
        map.put("ysid",ysid);
        OkHttpUtils.getNetWork(getActivity(), Constant.baseurl+"online/jzzz/sxzzzcDetail",map,handler);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    MyLogger.i(s);
                    iv_sx_img.setVisibility(View.INVISIBLE);
                    if(s.contains("id")){
                       // ll_roll_in.setVisibility(View.VISIBLE);
                        haverollin=true;
                    }else{
                      //  ll_roll_in.setVisibility(View.GONE);
                        haverollin=false;
                    }
                    Gson gson=new Gson();

                    try{
                        RollOutBean bean=gson.fromJson(s, RollOutBean.class);
                        tv_out_unit.setText("转出单位："+bean.getData().getZcjgmc());
                        tv_out_yinxiang.setText("初步现象："+bean.getData().getCbyx());
                        tv_out_xianbs.setText("现病史："+bean.getData().getXbs());
                        tv_out_jiwangshi.setText("既往史："+bean.getData().getJws());
                        tv_out_in_unit.setText("转入单位："+bean.getData().getDqjgmc());
                        tv_out_jingguo.setText("治疗经过："+bean.getData().getJljg());
                        tv_out_doctor.setText("转诊医生："+bean.getData().getZzysName());
                        tv_out_phone.setText("联系电话："+bean.getData().getYsdh());
                        tv_out_date2.setText("时间："+bean.getData().getZzss());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getActivity(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getActivity(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    /**
     * 转入请求网络
     */
    private void getNetWork1() {
        int ysid= (int) SpUtils.get(getActivity(),"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmId",jmid);
        map.put("jzid",jzid);
        map.put("ysid",ysid);
        OkHttpUtils.getNetWork(getActivity(), Constant.baseurl+"online/jzzz/sxzzzrDetail",map,handler1);
    }
    private Handler handler1=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    MyLogger.i(s);
                    iv_sx_img.setVisibility(View.INVISIBLE);
/*
                    if(s.contains("id")){
                        ll_roll_out.setVisibility(View.VISIBLE);
                        haverollout=true;
                    }else{
                        ll_roll_out.setVisibility(View.GONE);
                        haverollout=false;
                    }
                    if(!(haverollin&&haverollout)){
                        scollview_sx.setVisibility(View.GONE);
                        iv_sx_img.setVisibility(View.VISIBLE);
                    }else {
                        scollview_sx.setVisibility(View.VISIBLE);
                        iv_sx_img.setVisibility(View.GONE);
                    }*/
                    Gson gson=new Gson();
                    try{
                        RollInBean bean=gson.fromJson(s, RollInBean.class);
                        tv_referal_unit.setText("转入单位："+bean.getData().getZrjgmc());
                        tv_refer_result.setText("检查结果："+bean.getData().getJcjg());
                        tv_refer_fangan.setText("方案及建议："+bean.getData().getFajy());
                        tv_refer_doctor.setText("转诊医生："+bean.getData().getZzysName());
                        tv_refer_phone.setText("联系电话："+bean.getData().getLxdh());
                        tv_refer_date1.setText("时间：："+bean.getData().getZrss());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getActivity(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getActivity(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_referralrecords;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }
}
